package cn.fanqinghong.system.service.impl;

import cn.fanqinghong.system.domain.Role;
import cn.fanqinghong.system.mapper.PermissionMapper;
import cn.fanqinghong.system.mapper.RoleMapper;
import cn.fanqinghong.system.service.IRoleService;
import cn.fanqinghong.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fanqinghong
 * @since 2022-06-21
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {


    @Autowired
    private RoleMapper roleMapper;
    @Override
    @Transactional
    public void save(Role role) {
        roleMapper.save(role);
        initPermissionTable(role);
    }

    /**
     * 处理角色关联数据
     * @param role
     */
    private void initPermissionTable(Role role) {
        //1.清楚之前存储的数据
        //通过传进来的角色id删除menu和permission中的有关当前角色id的数据
        roleMapper.removeRoleMenuByRoleId(role.getId());
        roleMapper.removeRolePermissionByRoleId(role.getId());
        //2.新增，新传进来的数据
        roleMapper.saveRoleMenu(role);
        roleMapper.saveRolePermission(role);
    }

    @Override
    public void update(Role role) {
        roleMapper.update(role);
    }

    @Override
    public void removeById(Long id) {
        super.removeById(id);
    }

    @Override
    public void batch(List<Long> ids) {
        roleMapper.batch(ids);
    }
}
